﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJ.StreamSystem.LazyHare.Internal;


public class AggregateStreamSystemMessageListener(IAggregateStreamSystemHandlerInvoker aggregateStreamSystemHandlerInvoker, ILogger<IAggregateStreamSystemMessageListener> logger) : IAggregateStreamSystemMessageListener
{
    private readonly IAggregateStreamSystemHandlerInvoker _aggregateStreamSystemHandlerInvoker = aggregateStreamSystemHandlerInvoker;
    private readonly ILogger _logger = logger;

    public async Task<object> OnInitReceiveAsync(AggregateStreamSystemMessage message, CancellationToken cancellationToken)
    {
        try
        {
            cancellationToken.ThrowIfCancellationRequested();

            return await _aggregateStreamSystemHandlerInvoker.InitInvokeAsync(message);

        }
        catch (Exception ex)
        {
            _logger.LogWarning("[StreamSystem] Aggregate MessageListener  {message}", ex.Message);

            throw;
        }
    }


    public async Task<object> OnSummaryReceiveAsync(AggregateStreamSystemMessage message, CancellationToken cancellationToken)
    {
        try
        {
            cancellationToken.ThrowIfCancellationRequested();

            return await _aggregateStreamSystemHandlerInvoker.SummaryInvokeAsync(message);

        }
        catch (Exception ex)
        {
            _logger.LogWarning("[StreamSystem] Aggregate MessageListener  {message}", ex.Message);

            throw;
        }
    }


    public async Task<object> OnKeyReceiveAsync(AggregateStreamSystemMessage message, CancellationToken cancellationToken)
    {
        try
        {
            cancellationToken.ThrowIfCancellationRequested();

            return await _aggregateStreamSystemHandlerInvoker.KeyInvokeAsync(message);
        }
        catch (Exception ex)
        {
            _logger.LogWarning("[StreamSystem] Aggregate MessageListener  {message}", ex.Message);

            throw;
        }
    }
}